

import { createRouter, createWebHistory } from 'vue-router'
import { mapGetters } from 'vuex';

// import HomeworkLibrary from '../views/HomeworkLibrary.vue'
// import PaperLibrary from '../views/PaperLibrary.vue'
// import SubjectLibrary from '../views/SubjectLibrary.vue'

let myVuex = {...mapGetters(['getUserType'])};

console.log(myVuex.getUserType)

const routes = [
  // 运行项目时默认进入个人中心
 {
  path:'/',
  redirect:myVuex.getUserType=='student'?"/index/courseInfo":"/index/course"
 },
 {
  path:'/course',
  component:()=>import('../views/Course.vue')
 },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('../views/Login.vue'),
    },

 //课程细节默认展示公告模块
 {
  path:'/index/courseDetail',
  redirect:"/index/courseDetail/publicNotice"
 },

 //试卷与考试模块默认展示作业库模块
 {
  path:'/index/files',
  redirect:"/index/files/homeworkLibrary"
 },


 {
  path:'/index',
  component: () => import('../components/Index.vue'),
  children:[{
    //个人中心模块
    path:'personalCenter',
    component: () => import('../views/PersonalCenter.vue')
    
  },
  {
    //课程信息模块
    path:'courseInfo',
    component: () => import('../views/CourseInfo.vue')
    // component:CourseInfo
  },
  {
    //创建作业模块
    path:'createNewHomework',
    name:'createNewHomework',
    component: () => import('../views/CreateNewWork.vue'),
  },
  {
    //教师发布题库模块
    path:'files',
    component: () => import('../views/Files.vue'),
    children:[
      {
        //作业库模块
        path:'homeworkLibrary',
        name:'homeworkLibrary',
        // component:HomeworkLibrary
        component:()=>import('../views/HomeworkLibrary.vue')
      },{
        //试卷库模块
        path:'paperLibrary',
        name:'paperLibrary',
        // component:PaperLibrary
        component:()=>import('../views/PaperLibrary.vue')
      },{
        //题库模块
        path:'subjectLibrary',
        name:'subjectLibrary',
        // component:SubjectLibrary
        component:()=>import('../views/SubjectLibrary.vue')

      },
      {
        //题库模块
        path:'PublishPaper',
        name:'PublishPaper',
        // component:SubjectLibrary
        component:()=>import('../views/PublishPaper.vue')

      }
    ]
  },
    {
      //课程细节模块
    path:'courseDetail',
    component: () => import('../views/CourseDetail.vue'),
    name:'tocourseDetail',
    children:[{
      //公告模块
      path:'publicNotice',
      component: () => import('../views/PublicNotice.vue'),
      name:'topublicNotice'
    },{
      //作业模块
      path:'homework',
      component: () => import('../views/Homework.vue'),
      name:'tohomework'
    },{
      //试卷模块
      path:'test',
      component: () => import('../views/Test.vue'),
      name:'totest'
    },{
      //讨论区模块
      path:'talk',
     component: () => import('../views/Talk.vue'),
     name:'totalk',
    //  children:[{
    //      path:'details',
    //      name:'details',
    //      component: () => import('../views/Talk/Details.vue'),
    //    },
    //  ]
    },{

      path:'details',
      name:'details',
      component: () => import('../views/Talk/Details.vue'),
    },{
      //设置模块
      path:'settings',
      name:'settings',
      component: () => import('../views/Settings.vue'),
    },{
      //成员管理模块
      path:'manageStudent',
      name:'manageStudent',
      component: () => import('../views/ManageStudent.vue'),
    }]
  },{
    path:'course',
    component: () => import('../views/Course.vue'),
  }
]
},{
  //考试模块
  path: '/doingTest',
  component: () => import('../views/DoingTest.vue'),
}

]



const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

//配置全局的前置的路由守卫
router.beforeEach(function(to, from) {
    if (to.path == '/login') {
        console.log(from);
        return;
    } else {
        if (!localStorage.getItem("token")) { //没有
            //跳转到登录
            return { name: 'login' }
        }
    }
});
export default router






